package jman.teemwork12;

import java.sql.SQLException;
import java.util.HashMap;

public class UserManager {
	//已经登录的用户，用于登录以后的操作
	private HashMap<String,Business> loginUser=new HashMap<String,Business>();
	//注册
	public boolean registered(String name,String password){
		boolean result=false;
		User user=new User(name,password);
		DatebaseManager dm=new DatebaseManager();
		dm.getCon();
		dm.read("select * from user where username='"+user.getUserName()+"'");
		try {
			if(dm.getRs().next()){
				System.out.println("该用户名已被注册");
			}else{
				dm.add("insert into user (username,password) values ('"+user.getUserName()+"','"+user.getPassword()+"')");
				System.out.println("注册成功");
				result=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("注册失败");
		}
		dm.closeRs();
		dm.closePs();
		dm.closeCon();
		return result;
	}
	//登录
	public boolean login(String name,String password){
		boolean result=false;
		User user=new Business(name,password);
		DatebaseManager dm=new DatebaseManager();
		dm.getCon();
		dm.read("select * from user where username='"+user.getUserName()+"'");
		try {
			if(dm.getRs().next()){
				if(dm.getRs().getString("password").equals(user.getPassword())){
					result=true;
					System.out.println("登录成功");
					loginUser.put(user.getUserName(), (Business)user);
				}else{
					System.out.println("登录失败");
				}
			}else{
				System.out.println("登录失败");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("登录失败");
		}
		dm.closeRs();
		dm.closePs();
		dm.closeCon();
		return result;
	}
	//退出登录
	public boolean signout(String userName){
		boolean result=false;
		if(loginUser.containsKey(userName)){
			result=true;
			loginUser.remove(userName);
		}
		return result;
	}
	//开店
	public boolean OpenShop(String userName,String shopName){
		boolean result=false;
		if(loginUser.containsKey(userName)){
			DatebaseManager dm=new DatebaseManager();
			dm.getCon();
			dm.read("select * from shoplist where shopname='"+shopName+"'");
			try {
				if(dm.getRs().next()){
					System.out.println("该店名已被注册");
				}else{
					dm.add("insert into shoplist (shopname,business) values ('"+shopName+"','"+loginUser.get(userName).getUserName()+"')");
					System.out.println("店名注册成功");
					result=true;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				System.out.println("店名注册失败");
			}
			dm.closeRs();
			dm.closePs();
			dm.closeCon();
		}
		return result;
	}
	//判断是否有自己的网店
	public boolean isShop(String userName){
		boolean result=false;
		if(loginUser.containsKey(userName)){
			DatebaseManager dm=new DatebaseManager();
			dm.getCon();
			dm.read("select * from shoplist where business='"+userName+"'");
			try {
				if(dm.getRs().next()){
					result=true;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			dm.closeRs();
			dm.closePs();
			dm.closeCon();
		}
		return result;
	}
	//将商品数据存到数据库
	public boolean shopToDatabase(String userName,String shopName){
		boolean result=false;
		if(loginUser.containsKey(userName)){
			if(isShop(userName)){
				DatebaseManager dm=new DatebaseManager();
				dm.getCon();
				dm.read("select * from shoplist where business='"+userName+"' and shopname='"+shopName+"'");
				try {
					if(dm.getRs().next()){
						Business b=loginUser.get(userName);
						HashMap<String,Commodity> h=b.getShop().getHashMap();
						while(h.keySet().iterator().hasNext()){
							Commodity c=h.get(h.keySet().iterator().next().toString());
							dm.read("select * from shop where shopname='"+shopName+"' and commodityname='"+c.getCommodityName()+"' and (number!="+c.getNumber()+" or price!="+c.getPrice()+")");
							while(dm.getRs().next()){
								dm.update("update shop set number="+c.getNumber()+",price="+c.getPrice()+" where shopname='"+shopName);
							}
							dm.read("select * from shop where shopname='"+shopName+"' and commodityname='"+c.getCommodityName()+"' and number="+c.getNumber()+" and price="+c.getPrice());
							while(!dm.getRs().next()){
								dm.add("insert into shop (commodityname,number,price,shopname) values ('"+c.getCommodityName()+"',"+c.getNumber()+","+c.getPrice()+",'"+shopName+"')");
							}
							result=true;
						}
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				dm.closeRs();
				dm.closePs();
				dm.closeCon();
			}
		}
		return result;
	}
	//添加商品
	public boolean addCommodity(String userName,String shopName,Commodity commodity) throws Exception{
		boolean result=false;
		if(loginUser.containsKey(userName)){
			if(isShop(userName)){
				DatebaseManager dm=new DatebaseManager();
				dm.getCon();
				dm.read("select * from shoplist where business='"+userName+"' and shopname='"+shopName+"'");
				try {
					if(dm.getRs().next()){
						Business b=loginUser.get(userName);
						b.addCommodity(commodity);
						result=true;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				dm.closeRs();
				dm.closePs();
				dm.closeCon();
				shopToDatabase(userName,shopName);
			}
		}
		return result;
	}
	//修改商品的名字
	public boolean updateCommodityName(String userName,String shopName,Commodity commodity,String newCommodityName) throws Exception{
		boolean result=false;
		if(loginUser.containsKey(userName)){
			if(isShop(userName)){
				DatebaseManager dm=new DatebaseManager();
				dm.getCon();
				dm.read("select * from shoplist where business='"+userName+"' and shopname='"+shopName+"'");
				try {
					if(dm.getRs().next()){
						Business b=loginUser.get(userName);
						b.updateCommodityName(commodity, newCommodityName);
						result=true;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				dm.closeRs();
				dm.closePs();
				dm.closeCon();
				shopToDatabase(userName,shopName);
			}
		}
		return result;
	}
	//修改商品的数量
	public boolean updateCommodityNumber(String userName,String shopName,Commodity commodity,int newnumber) throws Exception{
		boolean result=false;
		if(loginUser.containsKey(userName)){
			if(isShop(userName)){
				DatebaseManager dm=new DatebaseManager();
				dm.getCon();
				dm.read("select * from shoplist where business='"+userName+"' and shopname='"+shopName+"'");
				try {
					if(dm.getRs().next()){
						Business b=loginUser.get(userName);
						b.updateCommodityNumber(commodity, newnumber);
						result=true;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				dm.closeRs();
				dm.closePs();
				dm.closeCon();
				shopToDatabase(userName,shopName);
			}
		}
		return result;
	}
	//修改商品的价格
	public boolean updateCommodityPrice(String userName,String shopName,Commodity commodity,double newPrice) throws Exception{
		boolean result=false;
		if(loginUser.containsKey(userName)){
			if(isShop(userName)){
				DatebaseManager dm=new DatebaseManager();
				dm.getCon();
				dm.read("select * from shoplist where business='"+userName+"' and shopname='"+shopName+"'");
				try {
					if(dm.getRs().next()){
						Business b=loginUser.get(userName);
						b.updateCommodityPrice(commodity, newPrice);
						result=true;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				dm.closeRs();
				dm.closePs();
				dm.closeCon();
				shopToDatabase(userName,shopName);
			}
		}
		return result;
	}
}
